const http = require("http");
const fs = require("fs");

const server = http.createServer((request, response) => {
  // 获取请求url的路径
  let { pathname } = new URL(request.url, "http://127.0.0.1");
  let filePath = __dirname + "/page" + pathname;

  fs.readFile(filePath, (err, data) => {
    if (err) {
      response.statusCode = 500;
      response.end("文件读取失败");
      return;
    }

    response.end(data);
  });
});

server.listen(9000, () => {
  console.log("服务已经启动...");
});
